//
//  YkNetWork.m
//  YuiKe
//
//  Created by yuike6 yuike6 on 11-11-16.
//  Copyright (c) 2011年 yuike. All rights reserved.
//

#import "YkNetWork.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
#import <SystemConfiguration/SCNetworkReachability.h>
#import "Toast+UIView.h"
//#import "Reachability.h"

@implementation YkNetWork
+ (BOOL)connectedToNetwork {
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags) {
        return NO;
    }
    
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    
    return (isReachable && !needsConnection) ? YES : NO;
}

+ (void) checkNetworkAvailable:(UIView*)view { // "baidu.com"
    
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        
        BOOL temp = ![self isDataSourceAvailable:@"baidu.com"];
        sleep(1.0f);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (temp) {
                [view makeToast:@"亲！网络貌似不可用喔" duration:TOAST_DRRATION_LONG position:TOAST_POSITION_TOPx];
            }
        });
    });
}
+ (BOOL)isDataSourceAvailable:(NSString *) hostName
{
//    static BOOL checkNetwork = YES;
    BOOL _isDataSourceAvailable = NO;
//    if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
//        checkNetwork = NO;
        
        Boolean success;    
//        const char *host_name = "twitter.com"; // your data source host name
        const char *host_name = [hostName UTF8String];
        
        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
        SCNetworkReachabilityFlags flags;
        success = SCNetworkReachabilityGetFlags(reachability, &flags);
        _isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
        CFRelease(reachability);
//    }
    return _isDataSourceAvailable;
}


//
//+(BOOL)isExistenceNetwork
//{
//    BOOL isExistenceNetwork;
//    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
//    switch ([r currentReachabilityStatus]) {
//        case NotReachable:
//            isExistenceNetwork=FALSE;
//            //   NSLog(@"没有网络");
//            break;
//        case ReachableViaWWAN:
//            isExistenceNetwork=TRUE;
//            //   NSLog(@"正在使用3G网络");
//            break;
//        case ReachableViaWiFi:
//            isExistenceNetwork=TRUE;
//            //  NSLog(@"正在使用wifi网络");        
//            break;
//    }
//    if (!isExistenceNetwork && TRUE) {
//        UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"网络不存在" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil,nil];
//        [myalert show];
//        [myalert release];
//    }
//    return isExistenceNetwork;
//}
@end
